/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.abstractvoid.gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
import pl.abstractvoid.languages.LanguageProvider;

/**
 *
 * @author Ivo Zieliński
 * @author Wojciech Szałapski
 */
public class PlotFrame extends javax.swing.JFrame {

    ImageIcon originalPlot;
    String titlePropertyName;
    MainFrame mediator;

    /**
     * Creates new form PlotFrame window with plots displayed in tabs.
     * @param plots
     * @param titlePropertyName
     * @param mediator
     */
    public PlotFrame(ArrayList<ImageIcon> plots, String titlePropertyName, MainFrame mediator) {
        this.titlePropertyName = titlePropertyName;
        this.mediator = mediator;
        setLayout(new GridLayout(1, 1));
        JTabbedPane plotTabbedPane = new JTabbedPane();
        plotTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        add(plotTabbedPane);
        int plotNumber = 1;
        for (ImageIcon plot : plots) {
            JLabel label = new JLabel();
            label.setIcon(plot);
            plotTabbedPane.addTab(LanguageProvider.getInstance().getString("plot") + " " + Integer.toString(plotNumber), label);
            plotNumber++;
        }
        pack();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle(LanguageProvider.getInstance().getString(titlePropertyName));
        setResizable(false);
        addWindowListener(new WindowEventHandler());
    }

    /**
     * Creates new form PlotFrame window a single plot chose from the gui. 
     * @param plot
     * @param titlePropertyName
     * @param mediator 
     */
    public PlotFrame(ImageIcon plot, String titlePropertyName, MainFrame mediator) {
        this.titlePropertyName = titlePropertyName;
        this.mediator = mediator;
        this.originalPlot = plot;
        initComponents();
        plotLabel.setIcon(plot);
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                processResizeEvent(e);
            }
        });
        Dimension size = new Dimension(plot.getImage().getWidth(null), plot.getImage().getHeight(null));
        setPreferredSize(size);
        setSize(size);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setTitle(LanguageProvider.getInstance().getString(titlePropertyName));
        setResizable(false);
        addWindowListener(new WindowEventHandler());
    }

    public void reloadTitle() {
        setTitle(LanguageProvider.getInstance().getString(titlePropertyName));
    }

    private void processResizeEvent(ComponentEvent e) {
        Image image = originalPlot.getImage().getScaledInstance(getWidth(), getHeight() - 10, Image.SCALE_SMOOTH);
        plotLabel.setIcon(new ImageIcon(image));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        plotLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(1, 1));
        getContentPane().add(plotLabel);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel plotLabel;
    // End of variables declaration//GEN-END:variables

    class WindowEventHandler extends WindowAdapter {

        public void windowClosing(WindowEvent evt) {
            mediator.plotFrames.remove(this);
        }
    }
}
